package cn.iqianye.app.qmc


import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.nio.charset.StandardCharsets


object Utils {
    fun showMyDialog(
        context: Context,
        title: String?,
        message: String,
        buttons: Map<String, DialogInterface.OnClickListener?>
    ) {
        val dialog: AlertDialog.Builder = AlertDialog.Builder(context)
        dialog.setTitle(title)
        dialog.setMessage(message)
        dialog.setCancelable(false)
        var i = 0
        buttons.forEach {
            if (i == 0) {
                dialog.setPositiveButton(it.key, it.value)
            } else {
                dialog.setNegativeButton(it.key, it.value)
            }
            i++
        }
        dialog.show()
    }

    @Throws(IOException::class)
    fun readInputStream(inputStream: InputStream): String? {
        val buffer = ByteArray(1024)
        var len: Int
        val bos = ByteArrayOutputStream()
        while (inputStream.read(buffer).also { len = it } != -1) {
            bos.write(buffer, 0, len)
        }
        bos.close()
        return String(bos.toByteArray(), StandardCharsets.UTF_8)
    }
}
